package cn.edu.jou.controller;

import cn.edu.jou.common.R;
import cn.edu.jou.dto.DishDto;
import cn.edu.jou.dto.SetmealDto;
import cn.edu.jou.entity.Category;
import cn.edu.jou.entity.Dish;
import cn.edu.jou.entity.Setmeal;
import cn.edu.jou.entity.SetmealDish;
import cn.edu.jou.service.CategoryService;
import cn.edu.jou.service.DishService;
import cn.edu.jou.service.SetmealDishService;
import cn.edu.jou.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息：{}",setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功");
    }

    @GetMapping("/page")
    //page:页码，pageSize:页面显示数量，name:前端传回来的查询条件
    public R<Page> page(int page, int pageSize, String name) {
        //构造分页构造器对象
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();

        //构造条件构造器
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据name进行查询，添加过滤条件
        lambdaQueryWrapper.like(name!=null,Setmeal::getName,name);

        //添加排序条件
        lambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);

        //执行分页查询
        setmealService.page(pageInfo,lambdaQueryWrapper);

        //SetmealDtoPage中没有值，要通过拷贝的方式来将pageInfo中的值拷贝到SetmealhDtoPage中
        //对象拷贝,但是要忽略掉Page中的records，因为该records对应的是list集合中的数据
        BeanUtils.copyProperties(pageInfo,setmealDtoPage,"records");

        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> list =
                records.stream().map((item)->{
                    SetmealDto setmealDto = new SetmealDto();
                    BeanUtils.copyProperties(item,setmealDto);
                    Long categoryId = item.getCategoryId();//分类ID
                    //根据id来查询菜品名称
                    Category category = categoryService.getById(categoryId);
                    if (category != null) {
                        String setmealName = category.getName();
                        setmealDto.setCategoryName(setmealName);
                    }
                    return setmealDto;
                }).collect(Collectors.toList());

        setmealDtoPage.setRecords(list);
        return R.success(setmealDtoPage);
    }

    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.getByIdWithDish(id);
        return R.success(setmealDto);
    }

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        log.info("setmealDto数据:{}",setmealDto);
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }

    //停售起售
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable int status,@RequestParam List<Long> ids){
        log.info(String.valueOf(status));
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //queryWrapper.eq(Dish::getId,ids);
        queryWrapper.in(Setmeal::getId,ids);
        setmealService.update(setmeal,queryWrapper);
            /*LambdaUpdateWrapper<Dish> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(Dish::getId,ids).set(Dish::getStatus,status);
            dishService.update(wrapper);*/
        return R.success("修改成功");

    }

    //删除操作，首先删除setmeal_dish表中数据，然后再删除setmeal表中数据
    @DeleteMapping
    public R<String> remove(@RequestParam List<Long> ids) {
        if (ids.isEmpty()) {
            return R.error("删除失败");
        }
        setmealService.removeWithDish(ids);
        return R.success("删除成功");
    }

}
